package com.xt.wikiperfumapi.service.mail.impl;

import com.xt.wikiperfumapi.service.mail.EmailService;
import jakarta.annotation.Resource;
import jakarta.mail.internet.MimeMessage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;

@Service("emailService")
@Slf4j
public class EmailServiceImpl implements EmailService {

    @Resource
    private JavaMailSender javaMailSender;

    @Value("${spring.mail.username}")
    private String mailUsername;

    public void sendMessageToEmail(String verifyCode, String email) {
        try{
            MimeMessage mimeMessage = javaMailSender.createMimeMessage();
            MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage, true);
            mimeMessageHelper.setFrom(mailUsername);
            mimeMessageHelper.setTo(email);
            mimeMessageHelper.setSubject("【WikiParfum】平台验证码邮件");
            String sb =
                    "<div style='background-color:#E9EAEF;height:280px;width:90vw;border:3px solid #B7B8CA;font-family:微软雅黑'>"+
                            "<h2>您的验证码为：</h2>" +
                            "<p style='color:red; text-align:center'><b> " + verifyCode + "</b>（有效期5分钟）</p>" +
                            "<p style='font-weight:bold; text-decoration:underline'>为了保证您的帐户安全，请勿向任何人提供此验证码。本邮件由系统自动发送，请勿直接回复。</p>"
                            +"</div>";
            mimeMessageHelper.setText(sb, true); // 关键是第二个参数，开启将富文本渲染为HTML
            javaMailSender.send(mimeMessage);
        }catch (Exception e){
            log.info(e.getMessage());
        }
    }

    public void sendMessageEmail(String title,String content,String name, String email) {
        try{
            MimeMessage mimeMessage = javaMailSender.createMimeMessage();
            MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage, true);
            mimeMessageHelper.setFrom(mailUsername);
            mimeMessageHelper.setTo(email);
            mimeMessageHelper.setSubject("【WikiParfum】平台"+title+"邮件");
            String sb =
                    "<div style='background-color:#E9EAEF;height:280px;width:90vw;border:3px solid #B7B8CA;font-family:微软雅黑'>"+
                            "<h2>尊敬的"+name+":</h2>" +
                            "<p style='font-weight:bold; '>"+content+"</p>"
                            +"</div>";
            mimeMessageHelper.setText(sb, true); // 关键是第二个参数，开启将富文本渲染为HTML
            javaMailSender.send(mimeMessage);
        }catch (Exception e){
            log.info(e.getMessage());
        }
    }

}

